package com.neulogistics.core.substation.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.core.substation.domain.model.aggregate.TaskOrder;
import com.neulogistics.core.substation.domain.model.enumeration.PaymentType;
import com.neulogistics.core.substation.domain.model.enumeration.TaskStatus;
import com.neulogistics.core.substation.domain.model.enumeration.TaskType;
import com.neulogistics.core.substation.domain.model.valueobject.CourierInfo;
import com.neulogistics.core.substation.domain.model.valueobject.CustomInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderItem;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.TaskOrderPO;
import org.springframework.beans.BeanUtils;

public class TaskOrderEntityPOConverter {
    private static final int Rate = 100;

    public static TaskOrderPO entityToPO(TaskOrder taskOrder){
        if(taskOrder == null)
            return null;

        TaskOrderPO taskOrderPO = new TaskOrderPO();
        // 复制普通属性
        BeanUtils.copyProperties(taskOrder, taskOrderPO);

        // 值对象转换
        if(taskOrder.getCourierInfo() != null){
            taskOrderPO.setCourierInfo(JSONUtil.parseObj(taskOrder.getCourierInfo()));
        }
        if(taskOrder.getCustomInfo() != null){
            taskOrderPO.setCustomInfo(JSONUtil.parseObj(taskOrder.getCustomInfo()));
        }
        if(taskOrder.getOrderInfo() != null){
            taskOrderPO.setOrderInfo(JSONUtil.parseObj(taskOrder.getOrderInfo()));
        }
        // 枚举值转换
        if(taskOrder.getTaskType() != null){
            taskOrderPO.setTaskType(taskOrder.getTaskType().id);
        }
        if(taskOrder.getTaskStatus() != null){
            taskOrderPO.setTaskStatus(taskOrder.getTaskStatus().id);
        }

        return taskOrderPO;
    }

    public static TaskOrder POToEntity(TaskOrderPO substationPaymentPO){
        if(substationPaymentPO == null)
            return null;

        TaskOrder substationPayment = new TaskOrder();
        // 复制普通属性, id是可以在这里被复制进去的
        BeanUtils.copyProperties(substationPaymentPO, substationPayment);
        // json对象转换为值对象
        substationPayment.setCourierInfo(JSONUtil.toBean(substationPaymentPO.getCourierInfo(), CourierInfo.class));
        substationPayment.setCustomInfo(JSONUtil.toBean(substationPaymentPO.getCustomInfo(), CustomInfo.class));
        substationPayment.setOrderInfo(JSONUtil.toBean(substationPaymentPO.getOrderInfo(), OrderInfo.class));
        // 枚举值转换
        substationPayment.setTaskType(TaskType.fromValue(substationPaymentPO.getTaskType(), TaskType.class));
        substationPayment.setTaskStatus(TaskStatus.fromValue(substationPaymentPO.getTaskStatus(), TaskStatus.class));

        return substationPayment;
    }

}
